<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<!--只在模块名下面有效 -->
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>拜访记录统计</title>
	<jsp:include page="../../import.jsp" flush="true" />
	<style>
		.visit_rec_detail_win {
			text-align: left !important;
			padding: 10px 25px 25px 25px;
		}
	</style>
</head>
<body class="iframeBody">
<div class="Wrapper">
	<div>
		<div id="pageInTab" class="pageInTab">
			<div tabid="tabA" title="拜访记录统计" lselected="true">
				<div class="searchArea">
					<form id="searchBar">
						<div class="fieldset1 clearFix">
							<div class="fieldBox">
								<div class="label">经销商</div>
								<div class="field">
									<input name="splNm" id="splNm" type="text" class="ui-textbox"
										   value="">
								</div>
							</div>
							<div class="fieldBox">
								<div class="label">拜访目的</div>
								<div class="field">
									<!-- <input name="visitPurpCd" id="visitPurpCd" type="text"
                                        class="ui-textbox" value=""> -->
									<select name="visitPurpCd" id="visitPurpCd" data-value=" ">
										<option value=" " checked>请选择</option>
										<c:forEach items="${purpCdMap}" var="kv">
											<option value="${kv.key}">${kv.value}</option>
										</c:forEach>
									</select>
								</div>
							</div>
							<div class="fieldBox">
								<div class="label">拜访角色</div>
								<div class="field">
									<select name="roleTypCd" id="roleTypCd" data-value=" ">
										<option value="1" checked>拜访人</option>
										<option value="2">协访人</option>
									</select>
								</div>
							</div>
							<div class="fieldBox">
								<div class="label">拜访时间</div>
								<div class="field">
									<input ligerui="width: 120" name="visitTmStar"
										   id="visitTmStar" type="text" class="ui-datepicker" value=""
										   validate="{required:true}"> - <input
										ligerui="width: 120" name="visitTmEnd" id="visitTmEnd"
										type="text" class="ui-datepicker" value=""
										validate="{required:true}">
								</div>
							</div>
							<div class="fieldBox col_33" id="cstMgrOrgCdId">
								<div class="label">分公司</div>
								<div class="field">
									<input name="cstMgrOrgCd" id="cstMgrOrgCd" type="text"
										   class="ui-textbox" value="">
								</div>
							</div>

							<div class="fieldBox ">
								<div class="label">负责人</div>
								<div class="field">
									<input name="crtUsrNm" id="crtUsrNm" type="text"
										   class="ui-textbox" value="">
								</div>
							</div>

							<div class="fieldBox">
								<div class="label">城市</div>
								<div class="field">
									<div class="field">
										<jsp:include page="../../system/sysRegion/region.jsp" flush="true" >
											<jsp:param name="regionShowLvl" value="2"/>
										</jsp:include>
									</div>
								</div>
							</div>
							<div class="clearFix"></div>
							<div class="btnsBox">
								<span id="searchBtn" class="sq-button sq-button-round">筛选</span>
								<span onclick="clearBtn();" class="sq-button sq-button-round">清空</span>
								<span onclick="exportVisitRec();"
									  class="sq-button sq-button-round">导出</span>
							</div>
						</div>
					</form>
				</div>
				<div id="ligergridDB" class="sq-grid"></div>

				<form id="backConfirm_form" style="display: none;">
					<div class="fieldset1 clearFix">
						<div class="fieldBox">
							<span id="tips"></span>
						</div>
						<div class="fieldBox">
							<div class="label">驳回原因</div>
							<div class="field">
									<textarea name="docketRem" id="docketRem"
											  class="ui-textarea l-textarea" rows="4" cols="60"
											  style="width: 260px;"
											  validate="{required:false,maxlength:200}"></textarea>
							</div>
						</div>
					</div>
				</form>

				<!-- 附件展示 -->
				<ul id="dowebok" style="display:none">
				</ul>

			</div>
			<c:if test="${buOrgCd == '00000001' }">
				<div tabid="tabB" title="关键经销商拜访记录统计">
					<iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
				</div>
			</c:if>
		</div>
	</div>
</div>

<script type="text/javascript">
    var gridDB = null;
    var pageInApp = {}; // to 下一级子窗口 接口
    var pageInTab = null;
    var loadedTabs = [];
    var visitRecTotal;
    var buOrgCd = '${buOrgCd}';
    $(function() {
        initTab();
        initGridDB(); //初始化待办列表
        initOrgTree();
        $("#searchBar").ligerForm();
        $("#searchBtn").click(function(event) {
            reloadGrid();
        });
        $('#closeAttachmentsView').click(function() {
            closeAttachmentsView();
        });
    });

    //重新加载GRID
    function reloadGrid() {
        var form = new liger.get("searchBar");
        var data = form.getData();
        for (name in data) {
            console.log('name:' + name + ',data:' + data[name]);
            gridDB.setParm(name, data[name]);
        }
        ;
        gridDB.reload();
    }

    //初始化列表
    function initGridDB() {
        $("#ligergridDB").ligerGrid({
            url : '<%=path%>/spl/splvisitrec/initList.do',
            method : 'get',
            columns : [
                {
                    display : '经销商名称',
                    name : 'splNm',
                    width : 80,
                    enabledSort : false
                },
                {
                    display : '地址',
                    name : 'visitPositAddr',
                    minWidth : 100,
                    enabledSort : false
                },
                {
                    display : '城市',
                    name : 'splRgnNm',
                    minWidth : 100,
                    enabledSort : false
                },
                {
                    display : '联系人',
                    name : 'conUsrNm',
                    minWidth : 150,
                    enabledSort : false,
                    render : function(rowdata, index, value) {
                        return value + "(" + rowdata.conTypCd + ")";
                    }
                },
                {
					display : '是否关键决策人',
					name : 'isRelDecision',
					minWidth : 100,
					enabledSort : false,
					render : function(rowdata, index, value) {
						if (value == 1) {
							return "是";
						}
						if (value == 0) {
							return "否";
						}
					}
				},
                {
                    display : '拜访目的',
                    name : 'visitPurpCd',
                    minWidth : 80,
                    enabledSort : false
                },

                {
                    display : '拜访时间',
                    name : 'crtTm',
                    minWidth : 120,
                    enabledSort : false,
                    render : function(rowdata, index, value) {
                        return renderDate2(rowdata, index, value)
                    }
                },
                {
                    display : '负责人',
                    name : 'crtUsrNm',
                    minWidth : 30,
                    enabledSort : false
                },
                {
					display : '职位',
					name : 'crtUsrPosition',
					minWidth : 30,
					enabledSort : false
				},
                {
                    display : '事业部',
                    name : 'buOrgCdNm',
                    minWidth : 100,
                    enabledSort : false
                },
                {
                    display : '省公司',
                    name : 'curPrOrgCdNm',
                    minWidth : 100,
                    enabledSort : false
                },
                {
                    display : '分公司',
                    name : 'curOrgCdNm',
                    minWidth : 100,
                    enabledSort : false
                },
                {
                    display : '签到距离',
                    name : 'signInDis',
                    minWidth : 30,
                    enabledSort : false,
                    render : function(rowdata, index, value) {
                        if (value == 0) {
                            return '0m';
                        }
                        if (value != null && value != '') {
                            return value + 'm';
                        }
                    }
                },
                {
                    display : '状态',
                    name : 'isValid',
                    minWidth : 50,
                    enabledSort : false,
                    render : function(rowdata, rowindex, value) {
                        if (rowdata.isValid == 1 && rowdata.isDel == 0) {
								return "有效";
							}
							if (rowdata.isValid ==0 && rowdata.isDel == 0) {
								return "审核中";
							}
							if (rowdata.isDel == 1) {
								return "无效";
							}
                    }
                },
                {
                    display : '拜访结果',
                    name : 'isSuccess',
                    minWidth : 50,
                    enabledSort : false,
                    render : function(rowdata, rowindex, value) {
                        if (value == 1) {
                            return "成功";
                        }
                        if (value == 0) {
                            return "失败";
                        }
                    }
                },
                {
                    display : '操作',
                    name : '',
                    minWidth : 200,
                    enabledSort : false,
                    render : function(rowdata, rowindex, value) {
                        var visitDetail;
                        var h1;
                        if (rowdata.visitDetail != null && rowdata.visitDetail != '') {
                            visitDetail = rowdata.visitDetail;
                            visitDetail = visitDetail.replace(/[\r\n]/g, "<br>");
                            h1 = '<span onclick="getVisitMessage(\'' + visitDetail + '\')" title="' + visitDetail + '"><a class="sq-button-hasicon" href="javascript:;">拜访详情</a></span>';
                        } else {
                            visitDetail = '';
                            h1 = '<span style="text-decoration:line-through"><a class="sq-button-hasicon">拜访详情</a></span>'
                        }
                        var _id = rowdata.id;
                        var h2 = '<a class="sq-button-hasicon" href="javascript:viewAttachments(' + rowdata.id + ')">查看附件</a>';
                        return h1 + h2;
                    }
                }
            ],
            pageSize : 10,
            pageSizeOptions : [ 10, 20, 30, 40, 50, 100 ], //按照自己的喜好选择不同的页面显示条数
            root : 'rows',
            record : 'total',
            hideLoadButton : true,
            width : '100%',
            height : '100%',
            heightDiff : -30,
            //checkbox : true,
            rownumbers : true,
            onAfterShowData : function() {
                $(".gridTip").grid_ligerTip();
                SetIframe_height();
            },
			onLoaded : function (grid) {
				console.log(grid);
				grid.toggleLoading(null);
				console.log(grid.data.total);
				if (null == visitRecTotal) {
					visitRecTotal = grid.data.total;
				}
			}
        });
        gridDB = liger.get("ligergridDB");
        gridDB.setHeight(100);
        gridDB.options.isSingleCheck = true;
    }

    <!-- 加载分公司下拉菜单选项 -->
    function initOrgTree() {
        var params = {};
        params.cstMgrOrgCd = '${currOrg}';
        searchOrgTree = $("#cstMgrOrgCd").ligerComboBox({
            width : 480,
            selectBoxWidth : 480,
            selectBoxHeight : 150,
            //限制只能选叶节点，父节点不能点击
            treeLeafOnly : false,
            valueField : 'orgCd',
            textField : 'orgDispNm',
            showTreePath : true,
            tree : {
                ajaxType : 'get',
                idFieldName : 'orgCd',
                textFieldName : 'orgDispNm',
                checkbox : false,
                parentIcon : "none",
                childIcon : "none",
                needCancel : false
            }
        });
        $.ajax({
            type : 'get',
            dataType : 'json',
            data : params,
            url : '<%=path%>/project/prjbscinfo/searchBuOrgTree.do',
            success : function(msg) {
                if (msg.success == 1) {
                    searchOrgTree.treeManager.setData(msg.data);
                    searchOrgTree.clear();
                    searchOrgTree.setValue('${currOrg}');
                } else {
                    $.ligerDialog.SQalert({
                        content : msg.info,
                        SQ_type : "error"
                    });
                }
            }
        });

    }
    
    <!-- 查看拜访详情 -->
    function getVisitMessage(visitDetail, event) {
        $.ligerDialog.SQalert({
            SQ_showButtons : true,
            width : 500,
            content : visitDetail
        });
    }

    <!-- 导出拜访记录 -->
    function exportVisitRec() {
        //alert();
        var wait_dialog;
        var form = new liger.get("searchBar");
        var data = form.getData();
        $.ajax({
            type : 'get',
            dataType : 'json',
            data : {
                'splNm' : data['splNm'],
                'visitPurpCd' : data['visitPurpCd'],
                'roleTypCd' : data['roleTypCd'],
                'visitTmStar' : data['visitTmStar'],
                'visitTmEnd' : data['visitTmEnd'],
                'cstMgrOrgCd' : data['cstMgrOrgCd'],
                'crtUsrNm' : data['crtUsrNm'],
                'rgnProvCd' : data['rgnProvCd'],
                'rgnCityCd' : data['rgnCityCd'],
				'visitRecTotal' : visitRecTotal
            },
            url : '<%=path%>/spl/splvisitrec/exportVisitRec.do',
            beforeSend : function() {
                wait_dialog = $.ligerDialog.SQalert({
                    content : '正在导出拜访记录信息...',
                    SQ_type : "waitting"
                });
            },
            success : function(msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    console.log(msg.data);
                    //window.open(msg.data);
                    window.location.href = msg.data;
                } else {
                    $.ligerDialog.SQalert({
                        content : msg.info,
                        SQ_type : "error"
                    });
                }
            }
        });
    }

    <!-- 查看附件 -->
    function viewAttachments(_id) {
        var urlArray = null;
        $.ajax({
            type : 'get',
            dataType : 'json',
            data : {
                id : _id
            },
            url : '<%=path%>/spl/splvisitrec/viewAttachments.do',
            beforeSend : function() {
                wait_dialog = $.ligerDialog.SQalert({
                    content : '正在查询附件...',
                    SQ_type : "waitting"
                });
            },
            success : function(msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    urlArray = msg.data;
                    if (urlArray != null) {
                        for (img in urlArray) {
                            var li = '<li><img  src="' + urlArray[img] + '" alt=""></li>';
                            $('#dowebok').append(li);
                        }
                        var viewer = new Viewer(document.getElementById('dowebok'), {
                            hidden : function() {
                                urlArray = null;
                                $('#dowebok').html('');
                                viewer.destroy();
                            },
                        });
                        viewer.show();
                    } else {
                        $.ligerDialog.SQalert({
                            content : '该记录没有附件...',
                            SQ_type : "warn"
                        });
                    }
                } else {
                    $.ligerDialog.SQalert({
                        content : msg.info,
                        SQ_type : "error"
                    });
                }
            }
        });
    }

    //格式化时间
    function renderDate2(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, "yyyy-MM-dd hh:mm");
        }
        return dateStr;
    }

    function clearBtn() {
        clearLigerForm("searchBar");
        $('#cstMgrOrgCdId span').text('');
    }

	//初始化Tab
	function initTab(){
		pageInTab = $("#pageInTab").ligerTab({
			height: 'auto',
			contextmenu: false,
			minIframeHeight: GET_minIframeHeight(),
			onBeforeSelectTabItem: function (tabid) {
				if(tabid!="tabA"){

				}
			},
			onAfterSelectTabItem: function(tabid) {
				if (loadedTabs.indexOf(tabid)<0 && buOrgCd && buOrgCd == "00000001") {
					loadedTabs.push(tabid);
					switch (tabid){
						case "tabB":
							pageInTab.setTabItemSrc(tabid, "<%=path%>/spl/splvisitrec/toListPage.do?isBisic=1");
							break;
					}
				};
				SetIframe_height();
			}
		});

		$.extend(true, pageInApp, {
			pageInTab: pageInTab
		});
	};
</script>

</body>
</html>